package com.ksm.client.document.presenter;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.IsWidget;
import com.ksm.client.ManagerEventBus;
import com.ksm.client.document.DocumentProperties;
import com.ksm.client.document.presenter.DocumentGridPresenter.IDocumentGridView;
import com.ksm.client.document.view.DocumentGridView;
import com.ksm.transfer.dto.DocumentDTO;
import com.mvp4g.client.annotation.Presenter;
import com.mvp4g.client.presenter.BasePresenter;
import com.mvp4g.client.view.ReverseViewInterface;
import com.sencha.gxt.data.shared.ListStore;

@Presenter(view = DocumentGridView.class)
public class DocumentGridPresenter extends BasePresenter<IDocumentGridView, ManagerEventBus> implements IDocumentGridPresenter {

	public interface IDocumentGridView extends IsWidget, ReverseViewInterface<IDocumentGridPresenter> {
		public void setListStore(ListStore<DocumentDTO> listeStore);

		public void addToStore(DocumentDTO dto);
	}

	private final DocumentProperties documentProperties = GWT.create(DocumentProperties.class);

	public void onDocumentSaved() {

	}

	/**
	 * Mise a jour de la liste des documents
	 * 
	 * @param documents
	 */
	@Override
	public void onDocumentGridUpdate(List<DocumentDTO> documents) {

		ListStore<DocumentDTO> documentStores = new ListStore<DocumentDTO>(documentProperties.key());
		documentStores.addAll(documents);

		getView().setListStore(documentStores);

	}

	@Override
	public void showDetailDocument(DocumentDTO dto) {
		getEventBus().documentGridLoad(dto);
	}

	@Override
	public void newDocument() {
		getEventBus().documentNewLoad();
	}

	public void onDocumentSaved(DocumentDTO dto) {
		getView().addToStore(dto);
		showDetailDocument(dto);
	}

}
